package com.huanwang.commonlibrary.utils

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.Proxy
import android.os.Build
import android.provider.Settings
import android.telephony.TelephonyManager
import android.text.TextUtils
import com.huanwang.commonlibrary.BuildConfig

/**
 * @author wang_hong_wei
 * @description 网络判断类
 * @Date 2018-5-11
 */
object NetUtils {
    /** 没有网络  */
    const val NETWORKTYPE_INVALID = 0

    /** wap网络  */
    const val NETWORKTYPE_WAP = 1

    /** 2G网络  */
    const val NETWORKTYPE_2G = 2

    /** 3G和3G以上网络，或统称为快速网络  */
    const val NETWORKTYPE_3G = 3

    /** wifi网络  */
    const val NETWORKTYPE_WIFI = 4
    private var mNetWorkType = 0

    /**
     * 获取网络状态，wifi,wap,2g,3g.
     *
     * @param context
     * 上下文
     * @return int 网络状态 [.NETWORKTYPE_2G],[.NETWORKTYPE_3G], *
     * [.NETWORKTYPE_INVALID],[.NETWORKTYPE_WAP]*
     *
     *
     * [.NETWORKTYPE_WIFI]
     */
    @SuppressLint("MissingPermission")
    fun isFastMobileNetwork(context: Context): Boolean {
        val telephonyManager = context
            .getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        return when (telephonyManager.networkType) {
            TelephonyManager.NETWORK_TYPE_1xRTT -> false // ~ 50-100 kbps
            TelephonyManager.NETWORK_TYPE_CDMA -> false // ~ 14-64 kbps
            TelephonyManager.NETWORK_TYPE_EDGE -> false // ~ 50-100 kbps
            TelephonyManager.NETWORK_TYPE_EVDO_0 -> true // ~ 400-1000 kbps
            TelephonyManager.NETWORK_TYPE_EVDO_A -> true // ~ 600-1400 kbps
            TelephonyManager.NETWORK_TYPE_GPRS -> false // ~ 100 kbps
            TelephonyManager.NETWORK_TYPE_HSDPA -> true // ~ 2-14 Mbps
            TelephonyManager.NETWORK_TYPE_HSPA -> true // ~ 700-1700 kbps
            TelephonyManager.NETWORK_TYPE_HSUPA -> true // ~ 1-23 Mbps
            TelephonyManager.NETWORK_TYPE_UMTS -> true // ~ 400-7000 kbps
            TelephonyManager.NETWORK_TYPE_EHRPD -> true // ~ 1-2 Mbps
            TelephonyManager.NETWORK_TYPE_EVDO_B -> true // ~ 5 Mbps
            TelephonyManager.NETWORK_TYPE_HSPAP -> true // ~ 10-20 Mbps
            TelephonyManager.NETWORK_TYPE_IDEN -> false // ~25 kbps
            TelephonyManager.NETWORK_TYPE_LTE -> true // ~ 10+ Mbps
            TelephonyManager.NETWORK_TYPE_UNKNOWN -> false
            else -> false
        }
    }

    /**
     * 获取网络的类型...如果是零代表没有网络
     * @param context
     * @return
     */
    fun getNetWorkType(context: Context): Int {
        val str: String? = null
        val manager = context
            .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = manager.activeNetworkInfo
        if (networkInfo != null && networkInfo.isConnected) {
            val type = networkInfo.typeName
            if (type.equals("WIFI", ignoreCase = true)) {
                mNetWorkType = NETWORKTYPE_WIFI
            } else if (type.equals("MOBILE", ignoreCase = true)) {
                val proxyHost = Proxy.getDefaultHost()
                mNetWorkType =
                    if (TextUtils.isEmpty(proxyHost)) if (isFastMobileNetwork(context)) NETWORKTYPE_3G else NETWORKTYPE_2G else NETWORKTYPE_WAP
            }
        } else {
            mNetWorkType = NETWORKTYPE_INVALID //没有网络
        }
        return mNetWorkType
    }

    /**
     * 判断是否有网络连接.....http://www.jianshu.com/p/10ed9ae02775
     * @param context
     * @return
     */
    fun isNetworkConnected(context: Context): Boolean {
        if (context != null) {
            // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
            val manager =
                context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            // 获取NetworkInfo对象
            val networkInfo = manager.activeNetworkInfo
            //判断NetworkInfo对象是否为空
            if (networkInfo != null) return networkInfo.isAvailable
        }
        return false
    }


    /**
     * 网络无连接时跳转页面.....http://blog.csdn.net/cy524563/article/details/41686735
     * @param context
     */
    fun showNoNetWorkDlg(context: Context) {
        val builder = AlertDialog.Builder(context)
        builder.setTitle(BuildConfig.appName) //
            .setMessage("网络未连接,是否去设置")
            .setPositiveButton("确定") { dialog, which -> // 跳转到系统的网络设置界面
                var intent: Intent? = null
                // 先判断当前系统版本
                if (Build.VERSION.SDK_INT > 10) {  // 3.0以上
                    intent = Intent(Settings.ACTION_WIRELESS_SETTINGS)
                } else {
                    intent = Intent()
                    intent.setClassName(
                        "com.android.settings",
                        "com.android.settings.WirelessSettings"
                    )
                }
                context.startActivity(intent)
            }
            .setNegativeButton("取消", null)
            .show()
    }
}